package zoujt.filters;


import zoujt.constants.Constant;
import zoujt.entitys.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/*
 * @auth zoujt
 * @date 2021/2/27
 * @Description
 */
@WebFilter("/*")
public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String uri = request.getRequestURI();
        if (uri.endsWith("index.jsp")||uri.endsWith("/sys/login/login")){

        }else {
            HttpSession session = request.getSession();
            User user =(User) session.getAttribute(Constant.SESSION_LOGIN);
            if (user == null){
                response.sendRedirect("/index.jsp");
                return;
            }
        }
        filterChain.doFilter(request,response);
    }
}
